#!/usr/bin/env python
# -*- encoding:utf-8 -*-
#**
#-------------------------------------------------------------------------------
#[Subject   -- SQL_Gen]
#[Author    -- tianxu.xxf]
#[Created   -- 2010.9.19]
#[Updated   -- ]
#[UpAuthor  -- ]
#-----------------------------------------------------------------------------*#
#from View import View
from Column import Column
from Directory import Directory
from Util.Tools import toColumn,toCondition
from SQLException.TypeException import TypeException
from Const._JoinType import RIGHT_OUTER_JOIN

class Table:
    """"""

    def __init__(self,name=''):
        self.__name              = name
        self.__partitions        = []
        self.__insert_from       = None
        self.__load_from         = None
        self.__is_load_overwrite = False

    def __str__(self):
        self_str = '\n{self: '+self.__name
        if self.__insert_from is not None:
            self_str += '\ninsert from:'
            self_str += str(self.__insert_from)
        if self.__load_from is not None:
            self_str += '\n'+'load from:'
            self_str += str(self.__load_from)
        self_str += '}'
        return self_str
    
    def SELECT(self,*columns):
        return View(self,*columns)

    def JOIN(self,view,joinType=None):
        new_view = View(self)
        new_view._View__addParent(view)
        return new_view

    def RIGHT_OUTER_JOIN(self,view):
        return self.JOIN(view,joinType=RIGHT_OUTER_JOIN)

    def INSERT_INTO(self,name,*partitions):
        table = Table(name)
        table._Table__insert_from = self
        table._Table__partitions = list(partitions)
        return table

    def INSERT_DIRECTORY(self,name,*partitions):
        directory = Directory(name)
        return directory
    
    def __getattr__(self,name):
        try:
            return object.__getattribute__(name)
        except:
            self.__dict__[name]=toColumn(name)
            return self.__dict__[name]

class View(Table):
    """"""   

    def __init__(self,table,*columns):
        if not isinstance(table,Table) and not isinstance(table,View):
            print 'error'
            raise TypeException
        Table.__init__(self,table._Table__name)
        self.__parents  = []
        self.__columns = []
        self.__condition = None
        self.__join_condition = None
        self.__order_by = []
        self.__sort_by = []
        
        self.__parents.append(table)
        self.__setColumn(columns)

    def __str__(self):
        self_str = '\n{self: (view from)'+self._Table__name
        if len(self.__parents) > 0:
            self_str += '\nparents:'
            for parent in self.__parents:
                self_str += str(parent)
        self_str += '}'
        return self_str
    
    def WHERE(self,condition):
        view = View(self)
        view._View__setCondition(condition)
        return view
    
    def ON(self,condition):
        view = View(self)
        view._View__setJoinCondition(condition)
        return view

    def ORDER_BY(self,*columns):
        view = View(self)
        view._View__setOrderBy(*columns)

    def SORT_BY(self,*columns):
        view = View(self)
        view._View__setSortBy(*columns)

    def __addParent(self,view):
        self.__parents.append(view)

    def __setColumn(self, columns):
        for column in columns:
            column = toColumn(column)
            self.__columns.append(column)
            self.__dict__[column._Column__name] = column
    
    def __setCondition(self,condition):
        self.__condition = toCondition(condition)

    def __setJoinCondition(self,condition):
        self.__join_condition = toCondition(condition)
        
    def __setOrderBy(self,*columns):
        map(self.__order_by.append,map(toColumn, columns))

    def __setSortBy(self,*columns):
        map(self.__sort_by.append,map(toColumn, columns))
